/********************************************************
peertool.js
A collection of functions common to most peertool pages.

This file is part of PeerTool, a web application for collaborative
decision-making (Copyrighted, 2009-2010, Ellis & Grant, Inc.)
Developed by Michael F. Ellis <michael.f.ellis@gmail.com>.  
License: AGPL v3
PeerTool comes with ABSOLUTELY NO WARRANTY.
********************************************************/
function AssertException(message) { this.message = message; }
    AssertException.prototype.toString = function () {
        return 'AssertException: ' + this.message;
    }

function assert(exp, message) {
    if (!exp) {
        throw new AssertException(message);
        }
    }

function plrl(n,singular,plural){
    if (Math.abs(n) != 1) return plural;
    else return singular;
    };

function fadeinout(selector) {
    $(selector).animate({
        opacity:0.4
        }, 'fast');
    $(selector).animate({
        opacity:1.0
        }, 'fast'); 
    };

function itemcomments(itemid,selector,caller,input) {
    // get comment list via JSON and
    // load it into the element identified by
    // selector and update the label for the
    // link identified by caller.
    var id = '#'+selector;
    /* if ($('#'+selector).css('display') == "none") {   */
        var url = PT.jsoncommenturl + "/" + itemid;
        $.getJSON(url, function(data) {
            if (data.comments.length > 0) {
                var commentlist = "<ul><li>"
                    + data.comments.join("</li><li>")
                    + "</li></ul>";
                }
            else { commentlist = ""};    
            $(id).html(commentlist);
            // update the link that called us.
            $('#'+caller).text(data.n.toString() 
                + " " + plrl(data.n, PT.T.comment, PT.T.comments));
            });
       /* }; */
       // toggle() not reliable in IE8 for nested elements,
       // so have to do it the hard way
       //$(id).toggle(); 
       if ($(id).css('display') == 'none') {
           $(id).show();
            }
       else {$(id).hide();} 

       if (PT.mode == "RESPOND") {
           //$('#'+input).toggle();
           var inpt = '#'+input;
           if ($(inpt).css('display') == 'none') {
               $(inpt).show();
                }
           else {$(inpt).hide();} 

       }

    };
        
function fulltexttip() {
    return ("Click to show/hide full text.");
    };

Array.prototype.sum = function() {
    for (var i = 0, L = this.length, sum = 0; i < L; sum += this[i++]);
    return sum;
    };

